<?php

namespace magein\utils;

class Client
{
    /**
     * @return string
     */
    public static function ip(): string
    {
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $realIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $realIp = $_SERVER['HTTP_CLIENT_IP'];
            } else {
                $realIp = $_SERVER['REMOTE_ADDR'];
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $realIp = getenv('HTTP_X_FORWARDED_FOR');
            } else if (getenv('HTTP_CLIENT_IP')) {
                $realIp = getenv('HTTP_CLIENT_IP');
            } else {
                $realIp = getenv('REMOTE_ADDR');
            }
        }
        return $realIp;
    }

    /**
     * @return bool
     */
    public static function isWin(): bool
    {
        $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
        if (empty($user_agent)) {
            return false;
        }
        if (preg_match('/(Windows|Win32|Win64|WinCE)/', $user_agent)) {
            return true;
        }
        return false;
    }

    /**
     * @return bool
     */
    public static function isLinux(): bool
    {
        $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
        if (empty($user_agent)) {
            return false;
        }
        if (preg_match('/(Macintosh|Mac OS X)/', $user_agent)) {
            return true;
        }
        return false;
    }
}